package com.ruoyi.readyProcurement.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.mapper.CompanyMapper;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.service.ReadyService;
import com.ruoyi.sale.service.ISaleorderService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/readyProcurement")
public class ReadyController extends BaseController {
    @Autowired
    ISaleorderService saleorderService;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ReadyService readyService;

    /*
    查询所有的产品信息
     */
    @GetMapping("/tableSelect")
    @ResponseBody
    public JSONObject tableSelect(@RequestParam(value = "productType", required = false) String productType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        HashMap map = new HashMap();
        map.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (productType != null & !"".equals(productType)) {
            map.put("productType", productType);
        }
        List<ProductModel> productModels = saleorderService.selectMateTableList(map);
        jsonObject.put("count", productModels.size());
        JSONArray jsonArray = new JSONArray();
        Long amount = 0L;
        for (int i = 0; i < productModels.size(); i++) {
            ProductModel productModel = productModels.get(i);
            if (productModel.getSalesUnit().equals("盒")) {
                String b = productModel.getWarehousingNum();
                int c = Integer.valueOf(b);
                amount = c / productModel.getFactor();
            } else {
                amount = Long.valueOf(productModel.getWarehousingNum());
            }
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("modelCode", productModel.getModelCode());
            jsonObject1.put("productId", productModel.getProduct().getProductId());
            jsonObject1.put("productName", productModel.getProduct().getProductName());
            jsonObject1.put("productType", productModel.getProductType());
            jsonObject1.put("unit", productModel.getSalesUnit());
            jsonObject1.put("manufacturerName", productModel.getManufacturerName());
            jsonObject1.put("warehousingNum", amount);
            jsonObject1.put("amount", productModel.getAmount());
            jsonObject1.put("factor", productModel.getFactor());
            jsonObject1.put("scopeId", productModel.getScopeId());
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("data", jsonArray);
        return jsonObject;
    }
             /*
             查询所有的供应商及其他信息
              */
    @GetMapping("/selectInformation")
    public Map<String, Object> add() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("sysUser", sysUser.getDeptId());
        resMap.put("userName", sysUserMapper.selectUserBycompanyId(sysUser.getCompanyId()));
        //供应商
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setCompanyId(sysUser.getCompanyId());
        resMap.put("supplier", mateManufactuterService.selectMateManufactuter(materialManufactuter));

        //税率
        Company company = companyMapper.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("taxRate", company.getTaxRate());
        return resMap;
    }

/*
保存预入库信息
 */
    @Log(title = "保存预入库信息", businessType = BusinessType.INSERT)
    @PostMapping("/insertReadyProcurement")
    public AjaxResult insertReadyProcurement(@RequestBody ReadyProcurement readyProcurement) {
        readyProcurement.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int a = readyService.addreadyProcurement(readyProcurement);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /*
    查询对应产品所需的原材料信息
     */
    @RequiresPermissions("business:product:list")
    @GetMapping("/selectProductRelation")
    @ResponseBody
    public Map<String, Object> selectProductRelation(@RequestParam(value = "productId",required = false) Long productId) {
        Map<String, Object> resMap = new HashMap<>();
        List<ProductRelation> relations = productMapper.selectProductRelation(productId);
        resMap.put("relations", relations);
        return resMap;
    }

  /*
  查询所有的预入库报告
   */
    @GetMapping("/ReadyProcurements")
    @ResponseBody
    public TableDataInfo selectReadyProcurements(@RequestParam(value = "beginTime", required = false) String beginTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime) {
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        ReadyProcurement readyProcurement = new ReadyProcurement();
        readyProcurement.setDeptId(deptId);
        readyProcurement.setBeginTime(beginTime);
        readyProcurement.setEndTime(endTime);
        startPage();
        List<ReadyProcurement> readyProcurements=readyService.selectReadyProcurements(readyProcurement);
        return getDataTable(readyProcurements);
    }

    /*
   查询已经做完的预入库报告
  */
    @GetMapping("/selectReadyProcurement")
    public Map<String, Object> selectReadyProcurement(@RequestParam(value = "readyId", required = false) Long readyId) {
        Map<String, Object> resMap = new HashMap<>();
        ReadyProcurement readyProcurement = readyService.selectReadyProcurement(readyId);
        resMap.put("readyProcurement", readyProcurement);
        return resMap;
    }

    /*
    编辑预入库报告
       */
    @PostMapping("/updateReadyProcurement")
    @ResponseBody
    public AjaxResult updateReadyProcurement(@RequestBody ReadyProcurement readyProcurement) {
        int num = readyService.updateReadyProcurement(readyProcurement);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }
    /**
     * 删除预入库报告
     */
    @Log(title = "删除预入库报告", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{readyId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("readyId") Long readyId) {
        readyService.remove(readyId);
        return AjaxResult.success("删除成功");
    }


    /*
     查询已经审核通过的预入库管理
      */
    @GetMapping("/auditCheckProcurement")
    public Map<String, Object> auditCourse() {
        Map<String, Object> resMap = new HashMap<>();
        List<ReadyProcurement> readyProcurements = readyService.auditCheckProcurement(SecurityUtils.getDeptId());
        resMap.put("result", readyProcurements);
        return resMap;
    }



}

